/**
 * Created by dukes on 2016/1/19.
 */
class CollectionPanel extends eui.Component{
    public constructor(){
        super();
        this.skinName = "pet.CollectionPanelSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAdded, this );
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE,this.onRemoved, this );
    }

    private onAdded(evt:egret.Event){
        this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.btn_collect.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCollect, this);
    }

    private onRemoved(evt:egret.Event){
        this.img_close.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.btn_collect.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onCollect, this);
    }

    public onClose(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.hide();
    }
    public onCollect(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        window["sendToDesktop"]();
        setTimeout(function(){
            window["__desktop"]();
        },100);
        this.hide();
    }

    public img_close:eui.Image;
    public btn_collect:eui.Image;
    public partAdded(name:string, instance:any) {
        super.partAdded(name, instance);
        if (name == "img_close") {
            this.img_close = instance;
        } else if (name == "btn_collect") {
            this.btn_collect = instance;

        }
    }
    public show() {
        DisplayController.instance().addToAlert(GameConst.GrayMask);
        DisplayController.instance().addToAlert(this);
    }
    public hide(){
        DisplayController.instance().removeFromAlert(GameConst.GrayMask);
        DisplayController.instance().removeFromAlert(this);
    }

    public measure(){
        super.measure();
        this.x = GameConst.StageW - this.measuredWidth >> 1;
        this.y = GameConst.StageH - this.measuredHeight >> 1;
    }

    private static _instance:CollectionPanel;
    public static getInstance():CollectionPanel{
        if( CollectionPanel._instance == null)
            CollectionPanel._instance = new CollectionPanel();
        return CollectionPanel._instance;
    }
}